package com.animation;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;

import com.view.ImagePanel;
import com.view.MainWindow;

public class DeparturePanel extends ImagePanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Image plane;
	private int x, y;
	private int height, anchor;
	private int resize = 0;
	private final int DELAY = 50;

	public DeparturePanel(){
		super(6);
		setDoubleBuffered(true);

		ImageIcon ii = new ImageIcon(
				MainWindow.class
						.getResource("/images/background/planeUpDown.png"));
		plane = ii.getImage();

		x = 0;
		y = -100;
		height = 53;
		anchor = 57;
		

		// TODO Auto-generated constructor stub
	}

	public void paint(Graphics g) {
		super.paint(g);

		Graphics2D g2d = (Graphics2D) g;
		
		g2d.drawImage(plane, x+40, y ,height+resize, anchor+resize, this);
		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}

	public void move() {
		y += 7;
		if(resize < 100) resize+=2;
	}

	public void animate() {
		//System.out.println("EJECUTANDO ANIMACION");
		long beforeTime, timeDiff, sleep;

		beforeTime = System.currentTimeMillis();
		//System.out.println(super.getHeight()+" "+this.y+" ");
		while (y < super.getHeight()) {
			//System.out.println(y);
			move();
			repaint();

			timeDiff = System.currentTimeMillis() - beforeTime;
			sleep = DELAY - timeDiff;

			if (sleep < 0)
				sleep = 2;
			try {
				Thread.sleep(sleep);
			} catch (InterruptedException e) {
				System.out.println("interrupted");
			}

			beforeTime = System.currentTimeMillis();

		}
		//reiniciamos el avion
		y = 0;
		resize = 0;
		repaint();
	}
}
